﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Vefskil5.Models;
using System.IO;
using System.Net;
using System.Configuration;
using System.Text;


namespace Vefskil5.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {

            var destFolder = ConfigurationManager.AppSettings["upload"];

            if (!Directory.Exists(destFolder))
            {
                Directory.CreateDirectory(destFolder);
            }
            IEnumerable<string> songPathList = Directory.GetFiles(ConfigurationManager.AppSettings["upload"]);


            List<TagLib.File> mp3files = new List<TagLib.File>();
            List<String> paths = new List<string>();
            foreach (var item in songPathList)
            {
                mp3files.Add(TagLib.File.Create(item));
                paths.Add(item);
            }

            int counter = 0;
            List<AudioBlaster3000> songs = new List<AudioBlaster3000>();
            foreach (var i in mp3files)
            {
                if (i.Tag.Title == null) i.Tag.Title = "";

                songs.Add(new AudioBlaster3000
                {
                    Id = counter,
                    Artist = i.Tag.FirstPerformer.ToString(),
                    Title = i.Tag.Title.ToString(),
                    Genre = i.Tag.FirstGenre.ToString(),

                    // This is the filename of the song and it is under the File Songs so we take what is in our song file and take the string from the last /
                    FileName = "Songs" + paths[counter].Substring(paths[counter].LastIndexOf('/'), paths[counter].Length - paths[counter].LastIndexOf('/'))
                });
                counter++;
            }
            return View(songs);
        }
    }
}
